﻿function reg_check(input)
{
    input = $(input);
    var id = input.attr("id");
	switch(id)
	{
		case "txtUserName":
		    if(input.val().length < 4){
			    $("#msgUserName")[0].className = "regtly_error";
			    $("#msgUserName").text("请输入4-20位用户名！");
			    return false;
			}else{			    
			    validate_username();
			}		   
		    break;
		case "txtPW":
		    if(input.val().length < 4){
			    $("#msgPW")[0].className = "regtly_error";
			    $("#msgPW").text("请输入6-20密码！");
			     return false;
			}else{
			    $("#msgPW")[0].className = "regtly_ok";
			    $("#msgPW").text("");
			}
		    break;
		case "txtRePW":
			if(input.val().length < 4){
			    $("#msgRePW")[0].className = "regtly_error";
			    $("#msgRePW").text("请输入确认密码！");
			     return false;
			}else if(input.val() != $("#txtPW").val()){
			    $("#msgRePW")[0].className = "regtly_error";
			    $("#msgRePW").text("两次输入密码不一致！");
			     return false;
			}else{
			    $("#msgRePW")[0].className = "regtly_ok";
			    $("#msgRePW").text("");			     
			}
			break;
	}
	return true;
}

function focus_input(input){
    input = $(input);
    var id = input.attr("id");
	switch(id)
	{
		case "txtUserName":		    
		    $("#msgUserName")[0].className = "regtly";
		    $("#msgUserName").text("4-20位字符或数字组合，必填。");
		    break;
		case "txtPW":
		    $("#msgPW")[0].className = "regtly";
		    $("#msgPW").text("6-20字符,数字或字母，区分大小写。");
		    break;
		case "txtRePW":			
		    $("#msgRePW")[0].className = "regtly";
		    $("#msgRePW").text("请再次输入您的密码。");
			break;
	}
}

function check_submit()
{
    var result = true;
    (!reg_check($("#txtUserName")[0]))?(result=false):"";
    (!reg_check($("#txtPW")[0]))?(result=false):"";
    (!reg_check($("#txtRePW")[0]))?(result=false):"";
    ($(".regtly_error").length > 0)?(result=false):"";
    return result;
}

function validate_username(){    
    $.get("/ValidateRegister.ashx",
    {"a":"ne","UName":$("#txtUserName").val()},
    function(data){        
        
        var result = eval("("+data+")");
        if(result.r == "y"){
             $("#msgUserName")[0].className = "regtly_error";
			 $("#msgUserName").text("用户名已存在！");
        }else{
            $("#msgUserName")[0].className = "regtly_ok";
		    $("#msgUserName").text("");
        }
    });
}
